
#include "scatter_elements_tiling.h"
#include "register/op_def_registry.h"

namespace optiling
{
    static ge::graphStatus TilingFunc(gert::TilingContext *context)
    {

        ScatterElementsTilingData tiling;

        auto shape_var = context->GetInputTensor(0)->GetOriginShape();
        auto shape_ind = context->GetInputTensor(1)->GetOriginShape();
        auto shape_upd = context->GetInputTensor(2)->GetOriginShape();

        int32_t size = 1;
        int32_t ind_size = 1;
        int32_t upd_size = 1;
        int32_t var_ndarray[10];
        int32_t ind_ndarray[10];
        int32_t var_dim, ind_dim, upd_dim;

        var_dim = shape_var.GetDimNum();
        ind_dim = shape_ind.GetDimNum();
        upd_dim = shape_upd.GetDimNum();

        for (int i = 0; i < var_dim; i++)
        {
            var_ndarray[i] = shape_var.GetDim(i);
            size *= var_ndarray[i];
        }

        for (int i = 0; i < ind_dim; i++)
        {
            ind_ndarray[i] = shape_ind.GetDim(i);
            ind_size *= ind_ndarray[i];
        }
        for (int i = 0; i < upd_dim; i++)
        {
            upd_size *= shape_upd.GetDim(i);
        }

        tiling.set_size(size);
        tiling.set_var_dim(var_dim);
        tiling.set_var_ndarray(var_ndarray);

        tiling.set_ind_size(ind_size);
        tiling.set_ind_dim(ind_dim);
        tiling.set_ind_ndarray(ind_ndarray);

        tiling.set_upd_size(upd_size);

        int axis = *context->GetAttrs()->GetInt(0);
        const char *reduction = context->GetAttrs()->GetStr(1);
        if (reduction[0] == 'a')
        {
            tiling.set_reduce(1);
        }
        else if (reduction[0] == 'm')
        {
            tiling.set_reduce(2);
        }
        else
        {
            tiling.set_reduce(0);
        }
        tiling.set_axis(axis);

        ge::DataType dataType = context->GetInputTensor(0)->GetDataType();
        if (dataType == ge::DT_FLOAT)
        {
            context->SetTilingKey(1);
        }
        else if (dataType == ge::DT_INT32)
        {
            context->SetTilingKey(2);
        }
        else if (dataType == ge::DT_FLOAT16)
        {
            context->SetTilingKey(3);
        }
        else if (dataType == ge::DT_UINT8)
        {
            context->SetTilingKey(4);
        }

        context->SetBlockDim(1);
        tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
        context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

        return ge::GRAPH_SUCCESS;
    }
}

namespace ge
{
    static ge::graphStatus InferShape(gert::InferShapeContext *context)
    {
        const gert::Shape *x1_shape = context->GetInputShape(0);
        gert::Shape *y_shape = context->GetOutputShape(0);
        *y_shape = *x1_shape;
        return GRAPH_SUCCESS;
    }
}

namespace ops
{
    class ScatterElements : public OpDef
    {
    public:
        explicit ScatterElements(const char *name) : OpDef(name)
        {
            this->Input("var")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_UINT8})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
            this->Input("indices")
                .ParamType(REQUIRED)
                .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
            this->Input("updates")
                .ParamType(REQUIRED)
                .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT32, ge::DT_UINT8})
                .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
                .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
            this->Attr("axis").AttrType(OPTIONAL).Int(0);
            this->Attr("reduce").AttrType(OPTIONAL).String("None");

            this->SetInferShape(ge::InferShape);

            this->AICore()
                .SetTiling(optiling::TilingFunc);
            this->AICore().AddConfig("ascend910b").AddConfig("ascend310b");
        }
    };

    OP_ADD(ScatterElements);
}
